package com.springboot.es.configs;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Configuration;

/**
 * @author sunxiaotao
 * @version 1.0
 * @date 2021-02-18 3:19 下午
 */
@Configuration
public class ElasticSearchConfig {

    private static final String urls = "localhost:9200";

    public RestHighLevelClient restHighLevelClient() {
        String[] list = urls.split(",");
        HttpHost[] httpHosts = new HttpHost[list.length];

        for (int i = 0; i < list.length; i++) {
            String item = list[i];
            String[] strings = item.split(":");
            httpHosts[i] = new HttpHost(strings[0], Integer.parseInt(strings[1]), "http");
        }

        return new RestHighLevelClient(RestClient.builder(httpHosts));
    }
}
